package com.banciyuan.bcywebview.base.view.imageview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.banciyuan.bcywebview.R;

/**
 * Created by squall on 2016/5/30.
 */
public class ScaleImageView extends ImageView {

    private float scale;

    public ScaleImageView(Context context) {
        super(context);
    }

    public ScaleImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        getAttr(context, attrs);
    }

    public ScaleImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        getAttr(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        height = (int) (width * scale);
        setMeasuredDimension(width, height);
    }

    private void getAttr(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ScaleImageView);
        scale = a.getFloat(R.styleable.ScaleImageView_scale, 1f);
    }


}
